<template>
  <a-card :bordered="false" class="card-area">
    <div class="table-page-search-wrapper">
      <a-form layout="vertical">
        <a-row :gutter="48">
          <a-col :md="6" :sm="12">
            <a-form-item label="院系">
              <a-select :disabled="selectMenu.yx.length === 1" :allowClear="true" showSearch :filterOption="filterOption" v-model="queryParam.ssyxm" @change="yxChange">
                <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{ i.dwh }} {{ i.dwmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item label="专业">
              <a-select :allowClear="true" showSearch :filterOption="filterOption" v-model="queryParam.sszym">
                <a-select-option v-for="i in selectMenu.zy" :key="i.id" :value="i.zyh">{{ i.zyh}} {{ i.zymc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item label="学年">
              <a-select :allowClear="true" v-model="queryParam.xn">
                <a-select-option v-for="i in TYPE0125" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item label="年级">
              <a-select :allowClear="true" v-model="queryParam.sznj">
                <a-select-option v-for="i in TYPE0126" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="6" :sm="12">
              <a-form-item label="学号">
                <a-input v-model="queryParam.xh"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="姓名">
                <a-input v-model="queryParam.xm"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="学生类别">
                <a-select :allowClear="true" v-model="queryParam.xslbm">
                  <a-select-option v-for="i in TYPE0075" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="欠费类型">
                <a-select :allowClear="true" v-model="queryParam.qflx">
                  <a-select-option v-for="i in TYPE0123" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <!-- <a-col :md="6" :sm="12">
              <a-form-item label="清欠费类型">
                <a-select :allowClear="true" v-model="queryParam.qqflx">
                  <a-select-option v-for="i in TYPE0123" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col> -->
          </template>
          <p style="color: red;float: left;margin-left: 20px;">温馨提示：该操作在清除欠费信息的同时，根据条件可能会修改注册状态为“已注册”，请慎用！</p>
          <a-col :md="10" :sm="24">
            <span class="table-page-search-submitButtons" >
              <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <a-button type="primary" @click="openAdd">清除欠费</a-button>
              <a-button style="margin-left: 8px" @click="() => queryParam = {ssyxm: selectMenu.yx.length === 1 ? queryParam.ssyxm : ''}">重置</a-button>
              <a @click="advanced = !advanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <s-table
      ref="table"
      size="default"
      rowKey="key"
      :columns="columns"
      :data="loadData"
      :alert="options.alert"
      :rowSelection="options.rowSelection"
      :pagination="{ pageSize: 10 }"
    >
      <span slot="serial" slot-scope="text, record, index">
        {{ index + 1 }}
      </span>

      <template slot="xh" slot-scope="a,b">
        <a @click="direct(b)">{{a}}</a>
      </template>

      <template slot="operation" slot-scope="a,b">
        <a @click="openEdit(b)">审核</a>
      </template>
    </s-table>

    <audit
      ref="audit"
      :visible.sync="visible"
      :editId.sync="editId"
      :TYPE0123="TYPE0123"
      @cancel="visible = false"
      @create="handleCreate"
    />
  </a-card>
</template>

<script>
// import moment from 'moment'
import { STable } from '~'
import { mapState } from 'vuex'
import audit from './audit'

export default {
  name: 'TableList',
  components: {
    STable,
    audit
  },
  data () {
    return {
      // 高级搜索 展开/关闭
      advanced: true,
      // 查询参数
      queryParam: {},
      postParams: {},
      // 表头
      columns: [
        {
          title: '学号',
          dataIndex: 'xh',
          scopedSlots: { customRender: 'xh' }
        },
        {
          title: '姓名',
          dataIndex: 'xm'
        },
        {
          title: '性别',
          dataIndex: 'xbmc'
        },
        {
          title: '年级',
          dataIndex: 'sznj'
        },
        {
          title: '学生类别',
          dataIndex: 'xslbmc'
        },
        {
          title: '院系',
          dataIndex: 'ssyxmc'
        },
        {
          title: '专业',
          dataIndex: 'sszymc'
        },
        {
          title: '学年',
          dataIndex: 'xn'
        },
        {
          title: '学费状态',
          dataIndex: 'jnxfzkmc'
        },
        {
          title: '住宿费状态',
          dataIndex: 'jnzsfzkmc'
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        if(this.selectMenu.yx.length > 0) {
          parameter.pageNum = parameter.pageNo
          let params = Object.assign(this.queryParam, parameter)
          return this.$api.student.clearArrearList(params).then(res => {
            return {
              data: res.data.rows,
              pageNo: parameter.pageNo,
              pageSize: parameter.pageSize,
              totalPage: Math.ceil(res.data.total / parameter.pageSize),
              totalCount: res.data.total
            }
          })
        }
      },
      selectedRowKeys: [],
      selectedRows: [],

      // custom table alert & rowSelection
      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      optionAlertShow: true, // 是否显示alert，已选中
      visible: false,
      confirmLoading: false,
      editId: null,
      selectMenu: {
        yx: [], // 院系
        zy: [] // 所属专业
      }
    }
  },
  computed: {
    ...mapState({
      systemInfo: state => state.app.systemInfo,
      TYPE0125: state => state.app['TYPE0125'],
      TYPE0126: state => state.app['TYPE0126'], // 年级
      TYPE0075: state => state.app['TYPE0075'], // 学生类别
      TYPE0056: state => state.app['TYPE0056'], // 录取类别
      TYPE0123: state => state.app['TYPE0123'], // 欠费类型
    })
  },
  created () {
    this.initSelectMenu()
    this.$store.dispatch('app/setDictionary', 'TYPE0125')
    this.$store.dispatch('app/setDictionary', 'TYPE0075')
    this.$store.dispatch('app/setDictionary', 'TYPE0126')
    this.$store.dispatch('app/setDictionary', 'TYPE0056')
    this.$store.dispatch('app/setDictionary', 'TYPE0123')
    this.$store.dispatch('app/sysInfo').then(res => {
      this.$set(this.queryParam, 'xn', this.systemInfo.schoolYear)
    })
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll()
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yx: res[0].data
        }
        if(this.selectMenu.yx && this.selectMenu.yx.length == 1){
          this.$set(this.queryParam, 'ssyxm', this.selectMenu.yx[0].dwh)
          this.yxChange(this.queryParam.ssyxm)
        }
        this.$nextTick(() => {
          this.$refs.table.refresh(true)
        })
      })
    },
    filterOption (value, op) {
      return op.componentOptions.children[0].text.indexOf(value) !== -1
    },
    handleCreate () {
      const form = this.$refs.audit.form
      form.validateFields((err, values) => {
        if (err) {
          return
        }
        this.$api.student.clearArrears(Object.assign({ids: this.id}, values)).then(res => {
          this.$message.warning('操作成功')
          this.$refs.table.refresh(true)
        })
        form.resetFields()
        this.visible = false
      })
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    direct (data) {
      this.$router.push(`/student/stuStatus/search/stuView?xh=${data.xh}`)
    },
    openAdd () {
      if (!this.selectedRowKeys.length) {
        this.$message.warning('请选择需要清除欠费的学生')
        return
      }
      this.id = this.selectedRows.map(v => {
        return v.id
      }).toString()
      this.visible = true
      this.$refs.audit.form.resetFields()
    },
    openEdit (row) {
      this.visible = true
      this.id = row.id
      this.$refs.audit.form.resetFields()
    },
    yxChange (code) {
      this.$set(this.queryParam, 'sszym', '')
      if (code) {
        this.$api.base.majorAll({lsdwh: code}).then(res => {
          this.$set(this.selectMenu, 'zy', res.data)
        })
      } else {
        this.$set(this.selectMenu, 'zy', [])
      }
    }
  }
}
</script>
